<!-- 143 -->
<template>
  <div>
    <a-space class="common-button-group">
      <a-button type="primary" :loading="searchBtnLoading" @click="getDataList('search')">{{ $t('common_query') }}</a-button>
    </a-space>
    <t-query-form layout="inline"
      ref="queryForm"
      group="PortHistory"
      :model="queryForm"
      >
      <a-row
        :gutter="[16, 0]"
      >
        <a-col :span="6">
          <a-form-item :label="$t('T9_shopId')" name="shopId" prop="shopId" :rules="{ required: true, message: $t('T9_shopId'), trigger: 'change'}">
            <a-select v-model:value="queryForm.shopId" class="select-width" @change="changeShopId" show-search>
              <a-select-option v-for="item in shopIdArr"
              :key="item.shopId" :value="item.shopId">
              {{item.shopId}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item :label="$t('T9_bayId')" name="bayId" prop="bayId" :rules="{ required: true, message: $t('T9_bayId'), trigger: 'change'}">
            <a-select v-model:value="queryForm.bayId" class="select-width" @change="changeBayId" show-search allow-clear>
              <a-select-option v-for="item in bayIdArr"
              :key="item.bayId" :value="item.bayId">
              {{item.bayId}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item :label="$t('T9_eqpId')" name="eqpId" prop="eqpId" :rules="{ required: true, message: $t('T9_eqpId'), trigger: 'change'}">
            <a-select v-model:value="queryForm.eqpId" class="select-width" @change="getPortIdList" show-search allow-clear>
              <a-select-option v-for="item in eqpIdArr"
              :key="item.eqpId" :value="item.eqpId">
              {{item.eqpId}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item :label="$t('T9_portId')" name="portId" >
            <a-select v-model:value="queryForm.portId" class="select-width" show-search allow-clear>
              <a-select-option v-for="item in portIdArr"
              :key="item.portId" :value="item.portId">
              {{item.portId}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item :label="$t('t9_date')" name="date">
            <a-range-picker
              :show-time="{ format: 'HH:mm:ss' }"
              :default-value="queryForm.Date"
              format="YYYY-MM-DD HH:mm:ss"
              @change="onChange"
            />
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item :label="$t('t9_eqpId')" name="date">
            <a-input v-model:value="queryForm.newEqpId" allow-clear></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </t-query-form>
    <a-row type="flex" justify="space-between" align="middle" class="global-oper-row mt-2">
      <a-col :span="15" class="global-oper-row-title">{{ $t('T9_portHistoryList') }}</a-col>
      <a-col :span="8" class="global-oper-row-extra">
        <a-button @click="getDataList('export')">{{ $t('t9_toExcel') }}</a-button>
      </a-col>
    </a-row>
    <div class="tableArea">
    <t-table
      :data="table.data" max-height="100%"
    >
      <t-column field="eventTime" :title="$t('T9_eventTime')" />
      <t-column field="eventName" :title="$t('T9_eventName')" />
      <t-column field="eventComment" :title="$t('T9_eventComment')" />
      <t-column field="eventUser" :title="$t('T9_eventUser')" />
      <t-column field="portState" :title="$t('T9_portState')" />
      <t-column field="transferState" :title="$t('T9_transferState')" />
      <t-column field="portTYpe" :title="$t('T9_portType')" />
      <t-column field="portId" :title="$t('T9_portId')" />
      <t-column field="accessMode" :title="$t('T9_accessMode')" />
      <t-column field="loadDspMode" :title="$t('T9_loadDspMode')" />
      <t-column field="unLoadDspMode" :title="$t('T9_unloadDspMode')" />
      <t-column field="portUseType" :title="$t('T9_portUseType')" />
      <t-column field="resourceState" :title="$t('T9_resourceState')" />
      <t-column field="cstId" :title="$t('T9_cstId')" />
      <t-column field="glassGrade" :title="$t('T9_glassGrade')" />
      <t-column field="mcsResourceState" :title="$t('T9_mcsResourceState')" />
      <t-column field="lastTransferStateChangeTime" :title="$t('T9_lastTransferStateChangeTime')" />
      <t-column field="estate" :title="$t('T9_estate')" />
      <t-column field="distantCallQty" :title="$t('T9_distantCallQty')" />
    </t-table>
    </div>

  </div>
</template>

<script>
  import { defineComponent } from 'vue'
  import moment from 'moment'
  import {
    getShopId,
    getBayId,
    getEqpId,
    getPortId,
    getOwnerType,
    getOwnerId,
    getList
  } from '@/api/common'
  import { getCurrentDate } from '@/utils/utils'
  import { downloadBlobFile } from '@/utils/download'

  export default defineComponent({
    name: 'PortHistory',
    components: {},
    data() {
      return {
        searchBtnLoading: false,
        table: {
          list: []
        },
        shopIdArr: [],
        bayIdArr: [],
        eqpIdArr: [],
        portIdArr: [],
        ownerTypeArr: [],
        ownerArr: [],
        queryForm: {
          shopId: 'CELL',
          bayId: '',
          eqpId: '',
          portId: '',
          Date: [],
          newEqpId: ''
        }
      }
    },
    computed: {},
    watch: {},
    created() {
      this.getBayId()
      this.getShopId()
      // this.getOwnerType()
      this.queryForm.Date = [moment(getCurrentDate(), 'YYYY-MM-DD HH:mm'), moment(getCurrentDate(1), 'YYYY-MM-DD HH:mm')]
    },
    methods: {
      onChange(value) {
        this.queryForm.Date = value
      },
      getDataList(type) {
        if (this.queryForm.newEqpId) {
          this.queryForm.eqpId = this.queryForm.newEqpId
        }
        const obj = {
          dtoName: 'PortHistoryDTO',
          startTime: this.queryForm.Date.length !== 0 ? this.queryForm.Date[0].format('YYYYMMDDHHmmss') * 1000000 : null,
          endTime: this.queryForm.Date.length !== 0 ? this.queryForm.Date[1].format('YYYYMMDDHHmmss') * 1000000 : null,
          eqpId: this.queryForm.eqpId,
          portId: this.queryForm.portId,
          export: false,
          queryId: 'GetPortHistoryList',
          version: '80002',
          menuName: 'portHistory',
          usage: 'queryPortHistoryList'
        }
        if (type === 'search') {
          obj.export = false
          getList(obj).then(res => {
            this.table = res
          })
        } else {
          obj.export = true
          getList(obj).then(res => {
            downloadBlobFile(res, 'portHistoryList')
          })
        }
      },
      handleSearch() {},
      changeShopId(val) {
        if (val) {
          this.getBayId()
        }
      },
      changeOwnerType(val) {
        if (val) {
          this.getOwner()
        }
      },
      changeBayId(val) {
        if (val) {
          this.getEqpId()
        }
      },
      getShopId() {
        const params = {
          userGroup: 'CellPIOper'
        }
        getShopId(params).then(res => {
          this.shopIdArr = res
        })
      },
      getBayId() {
        const params = {
          shopId: this.queryForm.shopId
        }
        getBayId(params).then(res => {
          this.bayIdArr = res
        })
      },
      getEqpId() {
        const params = {
          bayId: this.queryForm.bayId,
          shopId: this.queryForm.shopId,
          version: '80024'
        }
        getEqpId(params).then(res => {
          this.eqpIdArr = res
        })
      },
      getPortIdList() {
        const params = {
          eqpId: this.queryForm.eqpId
        }
        getPortId(params).then(res => {
          this.portIdArr = res
        })
      },
      getOwnerType() {
        getOwnerType().then(res => {
          this.ownerTypeArr = res
        })
      },
      getOwner() {
        const params = {
          ownerType: this.queryForm.ownerType,
          shopId: this.queryForm.shopId
        }
        getOwnerId(params).then(res => {
          this.ownerArr = res
        })
      }
    }
  })
</script>
<style lang="less" scoped>
.tableArea{
  width: 100%;
  height: 60vh;
}
</style>
